/*  -*- mode: c++; coding: utf-8; c-file-style: "stroustrup"; -*-

    Copyright 2008 Asier Aguirre <asier.aguirre@gmail.com>
    This file is part of Intromit.

    Intromit is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Intromit is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Intromit.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef OGL_FONT_HH
#define OGL_FONT_HH
//! \addtogroup introsight_framework
//@{

// INCLUDES
#include "util.hh"
#include "refcnt.hh"

#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_GLYPH_H
#include <X11/Xlib.h>

// DEFINES
#define OGL_FONT_N_CHAR     128
#define OGL_MAX_TEXT       1024


class ogl_font: public reference_counter
{
    unsigned int *textures;
    unsigned int list_base;
    float letter_params[OGL_FONT_N_CHAR][3]; // lenght in x, height in y, under_base in y

    // status of the font
    char *name;           // filename of FT font or X Font name
    float width, height;  // in pixels
    bool free_type;

    // PRIVATE METHODS
    void ctor();
    void dtor();

    bool init_bitmap(Display *dpy);
    bool init_free_type();

    bool make_dlist(FT_Face face, char ch);
    inline static int next_p2(int a) {int rval=1; while(rval<a) rval<<=1; return rval;}
    void clean_textures();
    void clean_lists();

public:
    ogl_font();
    ~ogl_font();
    bool init();
    bool init(const char *font_filename, float height); // FreeType font to be used as textured quads
    bool init(const char *font_name);           // X Font to be used as bitmap raster

    // scale to height units (destroys current gl matrix)
    float get_scale_font(float height);         // returns scale factor
    float scale_font(float height);             // returns scale factor, and do glScale
    inline float get_height() {return height;}
    inline float get_width() {return width;}    // for bitmap fonts

    // render text
    void write(const char *fmt, ...);

    // measure text
    void size(float params[3], const char *fmt, ...); // params[3]={lenght in x, height in y, under_base in y}
};

//@}
#endif

/* Offline metadata attributes:

   @metadata_attrs(
	ogl_font
	{
		# members
		height, name, free_type:
						select, read_only

		# methods
		init:
						select
	}
   )
*/
